package com.koalac.ironman.widget;

import com.koalac.ironman.util.ScreenInfoUtil;
import com.foot.travel.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View.MeasureSpec;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * @author 王政魁 2014年6月12日
 */
public class ClearTextEditView extends EditText {
	private Drawable dRight;
	private Rect rBounds;

	public ClearTextEditView(Context context) {
		super(context);
	}

	/**
	 * 初始化模板，设置背景，设置右边的图片
	 */
	public void initTemplate() {
		setSingleLine(true);
		setBackgroundResource(R.drawable.input_bg);
		Drawable right = getResources().getDrawable(R.drawable.edit_delete);
		// 设置输入框右边的图片
		setCompoundDrawablesWithIntrinsicBounds(null, null, right, null);
		change("", false);// 不让显示图片
	}

	public void setMaxLength(Integer maxLength) {
		setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });
	}

	public ClearTextEditView(Context paramContext,
			AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
	}

	public ClearTextEditView(Context paramContext,
			AttributeSet paramAttributeSet, int paramInt) {
		super(paramContext, paramAttributeSet, paramInt);
	}

	// 此方法会先于change(String,Boolean)方法执行，所以dRight不会为null。
	public void setCompoundDrawables(Drawable left, Drawable top,
			Drawable right, Drawable bottom) {
		if (right != null) {
			dRight = right;
		}

		super.setCompoundDrawables(left, top, right, bottom);
	}

	public boolean onTouchEvent(MotionEvent paramMotionEvent) {
		Log.i("ClearTextEditView", "点击了editView");
		if (this.dRight != null
				&& paramMotionEvent.getAction() == MotionEvent.ACTION_UP) {
			this.rBounds = this.dRight.getBounds();
			int x = (int) paramMotionEvent.getX();
			int y = (int) paramMotionEvent.getY();

			if (x >= (this.getWidth() - getPaddingRight() - rBounds.width())
					&& x <= (this.getWidth() - this.getPaddingRight())
					&& y >= this.getPaddingTop()
					&& y <= (this.getHeight() - this.getPaddingBottom())) {
				setText("");
				paramMotionEvent.setAction(MotionEvent.ACTION_CANCEL);
			}
		}
		return super.onTouchEvent(paramMotionEvent);
	}

	protected void change(String paramString, boolean paramBoolean) {
		if (this.dRight != null) {
//			
			if ((paramString.length() > 0) && (paramBoolean)) {
				setCompoundDrawables(null, null, this.dRight, null);
			}
			/*设置了setHint("")后，会导致输入法无法在第一次点击时弹出
			 * else if ((paramString.length() == 0) && (paramBoolean)) {
				this.setHint("");
				InputMethodManager input = (InputMethodManager) this.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
				input.showSoftInput(this, InputMethodManager.RESULT_SHOWN);
				
			}*/
			else{
				setCompoundDrawables(null, null, null, null);
			}
		}
	}
	@Override
	protected void onFocusChanged(boolean paramBoolean, int paramInt,
			Rect paramRect) {
		this.change(getText().toString(), paramBoolean);
		
		super.onFocusChanged(paramBoolean, paramInt, paramRect);
	}
	@Override
	protected void onTextChanged(CharSequence paramCharSequence, int paramInt1,
			int paramInt2, int paramInt3) {
		this.change(paramCharSequence.toString(), isFocused());
		super.onTextChanged(paramCharSequence, paramInt1, paramInt2, paramInt3);
	}
	/**
	 * 是为了适配该控件的宽度，高度则没有适配，为80dp
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		//这里要调用父类的onMeasure()方法，否则在部分机器上会有hint无法显示，没有selection等问题
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(measureWidth(widthMeasureSpec), 80);
//		Log.i("ClearTextE", this.getBackground().getBounds().height()+"");
	}
	private int measureWidth(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		if (specMode == MeasureSpec.EXACTLY) {
			// We were told how big to be
			result = specSize;
		} else {
			result = ScreenInfoUtil.getScreenWidth(this.getContext())*462/640;
			if (specMode == MeasureSpec.AT_MOST) {
				// Respect AT_MOST value if that was what is called for by
				// measureSpec
				result = Math.min(result, specSize);
			}
		}
		Log.i("CalendarView", specSize + "%%%" + specMode + "%%%%" + result);
		return result;
	}
	
	private int measureHeigth(int measureSpec){

		int result = 0;
	    int specMode = MeasureSpec.getMode(measureSpec);
	    int specSize = MeasureSpec.getSize(measureSpec);
	   
	    if (specMode == MeasureSpec.EXACTLY) {
	        // We were told how big to be
	        result = specSize;
	    } else {
//	    	result=this.getContext().getResources().getDrawable(R.drawable.input_bg).getBounds().height();
//	    	result=BitmapFactory.decodeResource(getResources(), R.drawable.input_bg).getHeight();
//	        result = ScreenInfoUtil.getScreenHeight(this.getContext())*80/1138;
	        if (specMode == MeasureSpec.AT_MOST) {
	            // Respect AT_MOST value if that was what is called for by measureSpec
	            result = Math.min(result, specSize);
	        }
	    }
	    Log.i("CalendarView1", specSize+"$$$$"+specMode+"####"+result);
	    return result;
	}

}
